from pyecharts import options as opts
from pyecharts.charts import Scatter, Tab

from .coordinate_convertion import gcj02_to_wgs84, wgs84_to_gcj02

__all__ = ['space_analyse', 'loc_trans']


def loc_trans(*loc, mode=0):
    """
    输入经纬度的数对，转换之后输出列表

    :param loc: 经纬度，可以输入多个
    :param mode: 转换模式，默认为火星转gps坐标，输入其他则反转
    :return: 返回列表，每个元素为转换后的经纬度
    """

    if mode:
        trans = wgs84_to_gcj02
    else:
        trans = gcj02_to_wgs84
    result = []
    for i in loc:
        result.append(trans(i[0], i[1]))

    return result


def space_analyse(emotion_kinds, emotion_list, space_data, mode=1):
    """
    将火星坐标转换为GPS坐标，并分别绘制五种情绪的坐标散点图

    :param emotion_kinds: 情绪种类，情绪向量各分量代表的情绪
    :param emotion_list: 情绪向量列表，emotion_analyse()的输出结果，参照emotion_analyse.py
    :param space_data: 地理数据，火星坐标，和情绪向量一一对应，格式为[经度,纬度]
    :param mode: 情绪分析模式，参照emotion_analyse.py
    """

    wgs84_data = loc_trans(*space_data, mode=0)
    wgs84_data.sort(key=lambda x: x[0])
    x_data = [d[0] for d in wgs84_data]
    y_data = [d[1] for d in wgs84_data]

    tab = Tab()

    for i in range(5):
        n = len(x_data)
        xi_data = [x_data[j] for j in range(n) if emotion_list[j][i] != 0]
        yi_data = [y_data[j] for j in range(n) if emotion_list[j][i] != 0]
        s = (
            Scatter(init_opts=opts.InitOpts(width="1526px", height="485px"))
                .add_xaxis(xaxis_data=xi_data)
                .add_yaxis(
                series_name="",
                y_axis=yi_data,
                label_opts=opts.LabelOpts(is_show=False),
            )
                .set_series_opts()
                .set_global_opts(
                xaxis_opts=opts.AxisOpts(
                    type_="value",
                    min_=116.11328,
                    max_=116.67098,
                    splitline_opts=opts.SplitLineOpts(is_show=True),
                    name="东经"
                ),
                yaxis_opts=opts.AxisOpts(
                    type_="value",
                    min_=39.71335,
                    max_=39.84894,
                    axistick_opts=opts.AxisTickOpts(is_show=True),
                    splitline_opts=opts.SplitLineOpts(is_show=True),
                    name="北纬"
                ),
                tooltip_opts=opts.TooltipOpts(is_show=False),
                title_opts=opts.TitleOpts(title="情绪-空间散点图")
            )
        )
        tab.add(s, emotion_kinds[i])

    tab.render("./output/space_analyse_{}.html".format(mode))
